<template>
  <div class="mod-menu">
    <el-form ref="pamarsRef" :inline="true" :model="pamars">
      <el-form-item label="会员手机号" prop="mobile">
        <el-input
          v-model="pamars.mobile"
          placeholder="会员手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="会员昵称" prop="nickname">
        <el-input
          v-model="pamars.nickname"
          placeholder="会员昵称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="会员身份" prop="grade">
        <el-select
          v-model="pamars.grade"
          clearable
          placeholder="请选择会员身份"
        >
          <el-option
            v-for="(item, index) in provinceArr"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="getDataList()">查询</el-button>
        <el-button type="warning" @click="resetPamars()">重置</el-button>
        <!-- <el-button type="primary" @click="exportAll()">导出数据</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      row-key="Id"
      border
      @sort-change="changeTableSort"
      style="width: 100%; "
    >
      <el-table-column prop="mobile" align="center" label="会员手机号">
      </el-table-column>
      <el-table-column prop="nickname" align="center" label="会员昵称">
      </el-table-column>
      <el-table-column prop="role_name" align="center" label="会员身份">
        <template slot-scope="{ row }">
          <div
            v-for="(v, i) in provinceArr"
            :key="i"
            v-if="v.value === row.grade"
          >
            {{ v.label }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="total_amount"
        sortable="custom"
        align="center"
        label="消费金额"
      >
      </el-table-column>
      <el-table-column
        prop="balance"
        sortable="custom"
        align="center"
        label="可用余额"
      >
      </el-table-column>
      <el-table-column
        prop="commission_able"
        sortable="custom"
        align="center"
        label="贡献值"
      >
        <template slot-scope="scoped">
          <div>
            {{ isToFixed(scoped.row.commission_able, 2) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="activating" align="center" label="激活状态">
        <template slot-scope="scope">
          <span>{{ scope.row.activating ? '激活' : '未激活' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="integral_able"
        sortable="custom"
        align="center"
        label="可用积分"
      >
        <template slot-scope="scoped">
          <div>
            {{ isToFixed(scoped.row.integral_able, 8) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="create_time"
        sortable="custom"
        align="center"
        label="注册时间"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >查看</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="activation(scope.row.id)"
          >激活</el-button
          >
          <!-- <el-button type="text" size="small" @click="changeLevel(scope.row)">设置等级</el-button> -->
          <!-- <el-button  type="text" size="small" @click="changeReferences(scope.row.id)">改变推荐人</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pamars.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pamars.limit"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!--查看 -->
    <user-details v-if="addOrUpdateVisible" ref="userDetails"></user-details>
    <set-level
      v-if="showSetLevel"
      ref="setLevel"
      :data="dataForm"
      @success="
        showSetLevel = false
        getDataList()
      "
    ></set-level>
    <!-- <user-references v-if="changeState" ref="userReferences"></user-references> -->
  </div>
</template>

<script>
import Vue from 'vue'
import userDetails from './user-details'
import setLevel from './set-level'
import seviceApi from '@/api/serviceApi.js'
// import userReferences from "./user-references.vue"
export default {
  data() {
    return {
      dataForm: {},
      pamars: {
        page: 1,
        limit: 10,
        mobile: '',
        nickname: '',
        roleCode: '',
        sort_field: '',
        sort_type: 'ASC'
      },
      totalPage: 0,
      dataList: [],
      selectList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      showSetLevel: false,
      // changeState:false,
      provinceArr: [
        { label: '普通会员', value: 0 },
        { label: '团长', value: 1 }
        // { label: '爱心小屋', value: 2 },
        // { label: '服务驿站', value: 3 },
        // { label: '服务中心', value: 4 },
        // { label: '市级', value: 5 },
        // { label: '省级公司', value: 6 },
        // { label: '大区公司', value: 7 }
      ]
    }
  },
  components: {
    userDetails,
    setLevel
    // userReferences
  },
  mounted() {
    this.getList()
    this.shopUserRoleConfig()
  },
  methods: {
    // 导出数据
    exportAll() {
      this.$confirm(`确定进行导出操作吗?`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let token = Vue.cookie.get('token')
          window.location.href = `${this.$config.SERVER_URL}/admin/account/shop/member/export?mobile=${this.pamars.mobile}&nickname=${this.pamars.nickname}&roleCode=${this.pamars.roleCode}&&token=${token}`
        })
        .catch(() => {})
    },
    // 会员等级
    statusFormatter(row, column) {
      let status = row.memberClass
      if (status === 0) {
        return '黄金会员'
      } else {
        return '铂金会员'
      }
    },
    shopUserRoleConfig() {
      seviceApi.shopUserRoleConfigselect().then(res => {
        if (res && res.data.code == 200) {
          this.selectList = res.data.list
        }
      })
    },
    getList() {
      seviceApi.getShopMemberlist(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    changeTableSort(e) {
      if (e.order == 'descending') {
        this.pamars.sort_type = 'DESC'
      } else if (e.order == 'ascending') {
        this.pamars.sort_type = 'ASC'
      }
      this.pamars.sort_field = e.prop
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList() {
      this.pamars.page = 1
      this.getList()
    },
    resetPamars() {
      this.pamars.sort_field = ''
      this.pamars.sort_type = 'ASC'
      this.$refs.pamarsRef.resetFields()
      this.getDataList()
    },
    // 查看
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.userDetails.init(id)
      })
    },
    activation(id) {
      seviceApi.activationUser(id).then(res => {
        if (res && res.data.result) {
          alert('激活成功！')
          // window.location.reload()
        }else{
          alert('激活失败！')
        }
      })
    },
    // 获取指定店铺的等级设置
    changeLevel(item) {
      this.dataForm = item
      this.showSetLevel = true
      this.$nextTick(() => {
        this.$refs.setLevel.visible = true
      })
    }
    // changeReferences(id){
    //   this.changeState = true
    //   this.$nextTick(() => {
    //     this.$refs.userReferences.init(id)
    //   })
    //   // console.log(id)
    // }
  }
}
</script>
